package com.azure.service.Report;

import com.azure.entity.AUserDailyReport;
import com.azure.entity.AUserMapReport;
import com.azure.entity.AUserMonthlyReport;
import com.azure.mapper.bigstore.TUserMapper;
import com.azure.mapper.report.AUserDailyReportMapper;
import com.azure.mapper.report.AUserMapReportMapper;
import com.azure.mapper.report.AUserMonthlyReportMapper;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Created by liuhuaibin on 2018/3/13.
 */
@Service
public class UserReportService {
    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private AUserMapReportMapper aUserMapReportMapper;
    @Autowired
    private AUserMonthlyReportMapper aUserMonthlyReportMapper;
    @Autowired
    private AUserDailyReportMapper aUserDailyReportMapper;

    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    public static SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyMM");
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyyMM");

    public void totalDailyReport(LocalDate date){
        List<JSONObject> jsonObjects = tUserMapper.totalDailyReport();
        System.out.println("====== UserReportService  totalDailyReport  jsonObjects: "+jsonObjects);
        for(JSONObject object : jsonObjects){
            AUserMapReport aUserMapReport = new AUserMapReport();
            aUserMapReport.setProvince(object.getAsString("province").replaceAll("省",""));
            aUserMapReport.setCity(object.getAsString("city").replaceAll("市",""));
            long count = (Long) object.getAsNumber("count");
            aUserMapReport.setCount(count*100);
            aUserMapReport.setMan((long) (count*100*0.7));
            aUserMapReport.setWomen((long) (count*100*0.3));
            aUserMapReport.setDate(dateTimeFormatter.format(date));
            //System.out.println("aUserMapReport: "+aUserMapReport);
            aUserMapReportMapper.insert(aUserMapReport);
        }
        List<JSONObject> dailyReportData = aUserMapReportMapper.getDailyReportData(dateTimeFormatter.format(date));
        System.out.println("====== UserReportService  totalDailyReport  dailyReportData: "+dailyReportData);
        for(JSONObject object : dailyReportData){
            AUserDailyReport aUserDailyReport = new AUserDailyReport();
            aUserDailyReport.setProvince(object.getAsString("province"));
            aUserDailyReport.setCount(Long.valueOf(object.getAsString("count")));
            aUserDailyReport.setDate(object.getAsString("date"));
            //System.out.println("aUserDailyReport: "+aUserDailyReport);
            aUserDailyReportMapper.insert(aUserDailyReport);
        }
    }
    public void totalMonthlyReport(LocalDate startDate, LocalDate stopDate){
        List<JSONObject> jsonObjects = tUserMapper.totalMonthlyReport();
        System.out.println("====== UserReportService  totalMonthlyReport  jsonObjects: "+jsonObjects);
        for(JSONObject object : jsonObjects){
            AUserMonthlyReport aUserMonthlyReport = new AUserMonthlyReport();
            aUserMonthlyReport.setDate(dateTimeFormatter2.format(startDate));
            aUserMonthlyReport.setProvince(object.getAsString("province"));
            aUserMonthlyReport.setCount((Long) object.getAsNumber("count"));
            aUserMonthlyReportMapper.insert(aUserMonthlyReport);
        }
    }

    public JSONArray getUserMapData(String date){
//        List<Map<String,Object>> cityList = new ArrayList<>();
        JSONArray array = new JSONArray();

        List<AUserMapReport> aUserMapReportList = aUserMapReportMapper.selectByDate(date);
        System.out.println("=====service aUserMapReportList:"+aUserMapReportList);
        for(AUserMapReport aUserMapReport : aUserMapReportList){
//            Map<String,Object> cityMap = new HashMap<>();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name",aUserMapReport.getCity());
            jsonObject.put("value",aUserMapReport.getCount());
            array.add(jsonObject);
//            cityMap.put("name","'"+aUserMapReport.getCity()+"'");
//            cityMap.put("value",aUserMapReport.getCount());
//            cityList.add(cityMap);
        }
        return array;
    }

    public Map<String,Object> getUserProvinceData(String date){
        List<String> provinceNameList = new ArrayList<>();
        List<Long> provinceValueList = new ArrayList<>();
        Map<String,Object> kv = new HashMap<>();

        List<JSONObject> provinceCount = aUserMonthlyReportMapper.selectProvinceCount(date);
        System.out.println("=====service provinceCount:"+provinceCount);
        for(JSONObject jsonObject : provinceCount){
            provinceNameList.add("\""+jsonObject.getAsString("province")+"\"");
            Object obj = jsonObject.getAsNumber("count");
            provinceValueList.add(Long.parseLong(obj.toString()));
        }
        kv.put("province",provinceNameList);
        kv.put("count",provinceValueList);
        kv.put("monthdate","\'"+date+"月数据\'");
        return kv;
    }

    public Map<String,Object> getDailyCountData(String date){
        List<String> dayStrList = new ArrayList<>();
        List<Long> dayCountList = new ArrayList<>();
        Map<String,Object> kv = new HashMap<>();

        List<JSONObject> userCount = aUserDailyReportMapper.selectDailyCount(date);
        System.out.println("=====service userCount:"+userCount);
        for(JSONObject jsonObject : userCount){
            dayStrList.add("\""+jsonObject.getAsString("date")+"\"");
            Object obj = jsonObject.getAsNumber("pcount");
            dayCountList.add(Long.parseLong(obj.toString()));
        }
        kv.put("date",dayStrList);
        kv.put("count",dayCountList);
        kv.put("daydate","\'"+dateTimeFormatter.format(LocalDate.now().minusDays(1))+"日数据\'");
        return kv;
    }



}
